package cn.ch.train.member.controller;

import cn.ch.train.common.response.CommonResponse;
import cn.ch.train.member.request.MemberLoginParam;
import cn.ch.train.member.request.MemberRegisterParam;
import cn.ch.train.member.request.MemberSendCodeParam;
import cn.ch.train.member.response.MemberLoginResult;
import cn.ch.train.member.service.MemberService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @GetMapping("/count")
    public CommonResponse<Long> count() {
        long count = memberService.count();
        return new CommonResponse<>(count);
    }

    @PostMapping("/register")
    public CommonResponse<Long> register(@Valid @RequestBody MemberRegisterParam param) {
        long id = memberService.register(param);
        return new CommonResponse<>(id);
    }

    @PostMapping("/send-code")
    public CommonResponse sendCode(@Valid @RequestBody MemberSendCodeParam param) {
        memberService.sendCode(param);
        return new CommonResponse<>();
    }

    @PostMapping("/login")
    public CommonResponse<MemberLoginResult> login(@Valid @RequestBody MemberLoginParam param) {
        MemberLoginResult result = memberService.login(param);
        return new CommonResponse<>(result);
    }
}
